/*
 * @Author: ✨Fireflx 
 * @Date: 2022-02-14 19:23:09 
 * @Last Modified by: ✨Fireflx
 * @Last Modified time: 2022-03-03 10:19:25
 */

import axios from 'axios'

const instance = axios.create({//🌟
    baseURL:'http://10.9.41.221:7000',
    timeout:3000
});

instance.interceptors.request.use(config=>{//🌟
    //请求拦截
    // const cookie = document.cookie.split('=')[1];
    const cookie = document.cookie.match(/user=\{(.*?)\}/)[0].split('=')[1];
    if(cookie){
        try {
            const user = JSON.parse(cookie)
            config.headers.common['Authorization'] = "Bearer "+user.token//🌟
        } catch (error) {
            console.error(error);
        }
    }
    return config//🌟
},err=>{//🌟
    console.error(err);
});

instance.interceptors.response.use(res=>{
    //响应拦截
    return res
},err=>{
    console.error(err);
})

async function http ({method,path,params}={}){//🌟
    let result;
    method = method.toLowerCase()
    await instance[method](path,params)
            .then(res=>{
                result = res.data
            }).catch(err=>{
                result = err
            })
    return result;
}

export default http;